package cn.fastdfs.pool;

import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

import cn.fastdfs.FastDFSClient;

public class ConnectionPool {
	private ConnectionPool pool = new ConnectionPool(null);
	private Object obj = new Object();
	private FastDFSClient client;
	// 被使用的连接
	private ConcurrentHashMap<StorageClient1, Object> busyConnectionPool = null;
	// 空闲的连接
	private ArrayBlockingQueue<StorageClient1> idleConnectionPool = null;

	public ConnectionPool(FastDFSClient client) {

	}

	private static ConnectionPool instance = new ConnectionPool();

	public static ConnectionPool getConnectionPool() {
		return instance;
	}

	// 取出连接
	public StorageClient1 getStorageClinet(int waitTime) {
		StorageClient1 storageClient1 = null;
		try {
			storageClient1 = idleConnectionPool.poll(waitTime, TimeUnit.SECONDS);
			if (storageClient1 != null) {
				busyConnectionPool.put(storageClient1, obj);
			}else{
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			storageClient1 = null;
			e.printStackTrace();
		}
		return storageClient1;
	}

	// 回收连接
	public void checkin(StorageClient1 storageClient1) {
		if (busyConnectionPool.remove(storageClient1) != null) {
			idleConnectionPool.add(storageClient1);
		}
	}

	// 如果连接无效则抛弃，新建连接来补充到池里
	public void drop(StorageClient1 storageClient1) {
		if (busyConnectionPool.remove(storageClient1) != null) {
			TrackerServer trackerServer = null;
			TrackerClient trackerClient = new TrackerClient();
			try {
				trackerServer = trackerClient.getConnection();
				StorageClient1 newStorageClient1 = new StorageClient1(trackerServer, null);
				idleConnectionPool.add(newStorageClient1);
			} catch (IOException e) {
				e.printStackTrace();
				close(trackerServer);
			} finally {
				close(trackerServer);
			}
		}
	}

	// 单例
	private ConnectionPool() {
		busyConnectionPool = new ConcurrentHashMap<StorageClient1, Object>();
		idleConnectionPool = new ArrayBlockingQueue<StorageClient1>(client.getMaxsize());
		init(client.getInitsize());
	}
	
	private void close(TrackerServer trackerServer){
		if (trackerServer != null) {
			try {
				trackerServer.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	// 初始化连接池
	private void init(int size) {
		TrackerServer trackerServer = null;
		try {
			TrackerClient trackerClient = new TrackerClient();
			// 只需要一个tracker server连接
			trackerServer = trackerClient.getConnection();
			StorageServer storageServer = null;
			StorageClient1 storageClient1 = null;
			for (int i = 0; i < size; i++) {
				storageClient1 = new StorageClient1(trackerServer, storageServer);
				idleConnectionPool.add(storageClient1);
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			close(trackerServer);
		} finally {
			close(trackerServer);
		}
	}
}
